//
//  ChangePhoneViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ChangePhoneViewController.h"

#import "TextFiledView.h"
@interface ChangePhoneViewController ()
@property (nonatomic,strong)NSTimer *timer;//定时器
@property (nonatomic,assign)int s;//秒
@property (nonatomic,strong)UIButton *sendBtn;//验证码按钮
@property (nonatomic,strong)NSString *phoneStr;//手机号
@property (nonatomic,strong)NSString *codeStr;//验证码
@property (nonatomic,strong)NSString *passStr;//密码
@end

@implementation ChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"修改手机号"];
    [self creadView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [IQKeyboardManager sharedManager].enable = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [IQKeyboardManager sharedManager].enable = NO;
}
-(void)creadView
{
    NSArray *titles = @[@"手机号",@"验证码"];
    for (int i = 0; i < titles.count; i++) {
        
        TextFiledView *textF = [[TextFiledView alloc]initWithFrame:CGRectMake(0,50*screenW_6*i, SCREENW, 50*screenW_6) PlaceHodel:titles[i] Title:titles[i] text:@""];
        textF.tag = 100+i;
        textF.backgroundColor = [UIColor whiteColor];
        textF.tfBlock = ^(NSString *tfStr){
            if (textF.tag == 100) {//手机号
                _phoneStr = tfStr;
            }else if (textF.tag == 101)//验证码
            {
                _codeStr = tfStr;
            }
        };
        [self.view addSubview:textF];
        
        
        if (i == 1) {
            NSString *str = @"点击获取";
            _sendBtn = [TomamaUI createButton:0 Frame:CGRectMake(SCREENW - 130*screenW_6 , 50*screenW_6, 100*screenW_6, 50*screenW_6) targ:self sel:@selector(sendMSG) titleColor:[UIColor colorWithHexString:@"#55aee9"] font:[UIFont systemFontOfSize:14*screenW_6] title:str image:nil backGroundImage:nil];
            [self.view addSubview:_sendBtn];
        }
    }
    
    UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(15*screenW_6, 135*screenW_6, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(makeShare) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16*screenW_6] title:@"提交" image:nil backGroundImage:nil];
    btn.backgroundColor = greenColor;
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 3*screenW_6;
    [self.view addSubview:btn];
}

#pragma mark - @selector

-(void)makeShare
{
    [self.view endEditing:YES];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    //验证手机号
    if (_phoneStr.length!=11) {
        [HudHelper hideHudToView:self.view];
        [TomamaUI creatAlertWithTarget:self message:@"请正确填写手机号码" timeInterval:2];
        return;
    }
    //手机验证码判空
    if (_codeStr.length <= 0) {
        [HudHelper hideHudToView:self.view];
        [TomamaUI creatAlertWithTarget:self message:@"请填写手机验证码" timeInterval:2];
        return;
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/login/updatePhone" Dic:@{@"id":[Storage getToken],@"phone":_phoneStr,@"verificationCode":_codeStr} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:[standfault objectForKey:@"userInfo"]];
                    [userInfo setObject:_phoneStr forKey:@"phone"];
                    [standfault setObject:userInfo forKey:@"userInfo"];
                    [standfault synchronize];
                    [TomamaUI creatAlertWithTarget:self message:@"修改成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [HudHelper hideHudToView:self.view];
            });
        }];
    });
    
}

-(void)sendMSG//发送验证码
{
    _sendBtn.userInteractionEnabled = NO;
    if (_phoneStr.length==11) {//手机验证11位
        _s = 60;
        [_sendBtn setTitle:@"60秒后重发" forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        [HttpRequest PostrequestUrl:@"/login/loginSendverificationCode" Dic:@{@"phone":_phoneStr} completion:^(NSDictionary *responseDic) {
            if (![responseDic[@"success"] boolValue]) {//请求失败 重置定时器 及按钮
                [HudHelper showHudWithMessage:responseDic[@"msg"]?responseDic[@"msg"]:@"验证码发送失败" toView:self.view];
                [_sendBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                _sendBtn.userInteractionEnabled = YES;
                [_timer invalidate];
                _timer = nil;
            }
        }];
    }
    else//验证失败
    {
        [ToolViewVC addKeyAlterViewController:@"手机号码格式不正确" Controller:self];
        _sendBtn.userInteractionEnabled = YES;
    }
}
//定时器
-(void)onTimer
{
    _s--;
    NSString *timeStr = [NSString stringWithFormat:@"%d秒后重发",_s];
    [_sendBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_s==0)//秒数为0 重置按钮定时器
    {
        [_sendBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        _sendBtn.userInteractionEnabled = YES;
        [_timer invalidate];
        _timer = nil;
    }
}

@end
